package com.fy.sys.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fy.core.context.AuthContext;
import com.fy.core.enums.ApiErrorEnum;
import com.fy.core.exception.ServiceException;
import com.fy.core.util.JWTUtil;
import com.fy.core.vo.ApiRest;
import com.fy.sys.dto.UserPasswordDto;
import com.fy.sys.entity.User;
import com.fy.sys.mapper.UserMapper;
import com.fy.sys.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.ServletException;
import java.util.Map;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author hxd
 * @since 2023-06-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ApiRest login(User user, String code, String id) {

        //1:参数校验

        //2:校验验证码
        verfyCode(code, id);

        //3：校验账号是否合法
        User userDB = verifyUser(user);

        //4:颁发令牌
        //颁发jwt令牌
        userDB.setPassword("");
        String token = JWTUtil.generatorMINUTEToken(userDB, 30);

        return ApiRest.success(token);
    }

    private void verfyCode(String code, String id) {

        Map map = userMapper.selectCodeById(id);
        String codeDB = (String) map.get("code");
        if (StringUtils.isEmpty(code)){
            throw new ServiceException(ApiErrorEnum.LOGIN_INVALID_MSG);
        }
        if (!code.equals(codeDB)){
            throw new ServiceException(ApiErrorEnum.LOGIN_INVALID_MSG);
        }

        //删除code
    }

    private User verifyUser(User user) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUserName, user.getUserName());

        User userDB = this.baseMapper.selectOne(queryWrapper);
        if (userDB == null){
            throw new ServiceException(ApiErrorEnum.LOGIN_USERNAME_PASSWORD_INVALID);
        }


        //比较密码
        if (!DigestUtil.bcryptCheck(user.getPassword(), userDB.getPassword())){
            throw new ServiceException(ApiErrorEnum.LOGIN_USERNAME_PASSWORD_INVALID);
        }

        return userDB;

    }

    @Override
    public ApiRest updateUserProfile(User user) {
        User userDB = userMapper.selectById(user.getUserId());
        String password = userDB.getPassword();
        user.setPassword(password);
        userMapper.updateById(user);
        return ApiRest.success("修改用户信息成功","");
    }

    @Override
    public ApiRest updateUserPwd(UserPasswordDto userPasswordDto) {

        String userId = AuthContext.getUser().getUserId()+"";

        User userDB = userMapper.selectById(userId);
        String password = userDB.getPassword();

        if(!DigestUtil.bcryptCheck(userPasswordDto.getOldPassword(),password)){
            throw new ServiceException(ApiErrorEnum.OLD_PASSWORD_INPUT_DIFFERENT);
        }

        String bcryptPassword = DigestUtil.bcrypt(userPasswordDto.getNewPassword());
        userDB.setPassword(bcryptPassword);

        userMapper.updateById(userDB);

        return ApiRest.success("修改密码成功","");
    }

    public static void main(String[] args) {

        //注册
        String bcrypt = DigestUtil.bcrypt("123");
        System.out.println("bcrypt = " + bcrypt);

        String pwdDB="$2a$10$7RoxxxibmADkOtcsHzYovehX5D/aHIeBOoIr6UmFfTQf2eS0v5Kv6";
        boolean b = DigestUtil.bcryptCheck("321", pwdDB);
        System.out.println("b = " + b);

    }




}
